/* m_fileDir由char数组改为string;m_bodygarmentlist和m_boundaryseg修改为vector数组
 * humansimulation2的filepath变量弃用
 * 用BufferedWriter写文件(通常writer会比outputstream效率高)
 * strcpy改为new String()
 * 弃用：humansimulation
 * 暂时注释:load,save方法,presimulation
 */
package com.simulation;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.simulation.All.ModelType;

class SimulateData{
	static enum SEG_INDEX{HEAD, TRUNK, ARM, HAND, LEG, FOOT};
	static final String FIBERNAME[]={"Cotton","Nylon","Polypropylene","Polyester","Wool"};
	/* 身体特征 */
	static final double C[]={2.57,0.39,0.26,0.28,11.44,18.80,4.94,1.41,1.63,3.54,6.7,0.30,
	                    0.16,0.7,0.10,0.20,4.93,10.67,1.66,1.25,0.27,0.07,0.15,0.26,2.60
	                   };
	static final double QB[]={14.95,0.12,0.13,0.10,52.63,5.81,2.49,0.47,0.82,1.11,0.21,0.15,
	                     0.09,0.23,0.04,0.06,2.59,3.32,0.50,0.37,0.15,0.02,0.05,0.08
	                    };
	static final double EB[]={0.0,0.0,0.0,0.81,10.45,0.0,0.0,3.78,0.0,0.0,0.0,1.40,0.0,
	                     0.0,0.0,0.52,0.0,0.0,0.0,3.32,0.0,0.0,0.0,0.72
	                     };
	static final double BFB[]={45.0, 0.12, 0.13, 1.44, 210.0, 6.0, 2.56, 2.10, 0.84, 1.14, 0.2, 0.5, 0.1, 0.24,
	                      0.04, 2.0, 2.69, 3.43, 0.52, 2.85, 0.16, 0.02, 0.05, 3.0
	                     };
	static final double TC[]={1.61, 13.25, 16.1, 0.0, 1.59, 5.53, 23.08, 0.0, 1.4, 10.3, 10.5, 0.0, 6.4,
	                     11.2, 11.5, 0.0, 10.5, 14.4, 74.5, 0.0, 16.3, 20.6, 16.4, 0.0
	                    };
	static final double S[]={0.1326, 0.6804, 0.2536, 0.0946, 0.5966, 0.1299};
	static final double HC[]={3.0, 3.1, 2.1, 2.0, 2.1, 4.0};
	static final double TSET[]={36.96, 35.07, 34.81, 34.58, 36.89, 36.28, 34.53, 33.62, 35.53, 34.12,
	                       33.59, 33.25, 35.41, 35.38, 35.30, 35.22, 35.81, 35.30, 35.31, 34.10,
	                       35.14, 35.03, 35.11, 35.04, 36.71
	                      };
	static final double SKINR[]={0.21, 0.42, 0.10, 0.04, 0.20, 0.03};
	static final double SKINS[]={0.081, 0.481, 0.154, 0.031, 0.218, 0.035};
	static final double SKINV[]={0.132, 0.322, 0.095, 0.121, 0.230, 0.10};
	static final double SKINC[]={0.05, 0.15, 0.05, 0.35, 0.05, 0.35};
	static final double MWORK[]={0.0, 0.3, 0.08, 0.01, 0.60, 0.01};
	static final double MCHILL[]={0.02, 0.85, 0.05, 0.0, 0.07, 0.0};
	static final double T[]={36.96, 35.07, 34.81, 34.58, 37.0, 36.28, 34.53, 33.62, 35.53, 34.12, 35.59,
	                    33.25, 35.41, 35.38, 35.30, 35.22, 35.81, 35.30, 35.31, 34.10, 35.14, 35.03,
	                    35.11, 35.04, 36.71
	                   };
	
	static final double RH_REGAIN_X[]={0.0, 0.0667, 0.1333, 0.2, 0.2667, 0.333, 0.4, 0.4667,
        	0.5333,0.6, 0.6667, 0.7333, 0.8, 0.8667, 0.9333, 1.0};
	static final double RH_REGAIN_Y[][]={
	/*0--Cotton*//*{0.0000, 0.02, 0.03, 0.04,0.045, 0.055, 0.0488, 0.0552,0.0602, 0.0705, 0.0800, 0.0920, 0.1060, 0.1275, 0.1720, 0.2400}*/
	{0.0,0.02,0.03,0.04,0.045,0.055,0.06,0.065,0.07,0.075,0.08,0.10,0.12,0.16,0.19,0.25},
	/*1--Nylon*/ {0.0000, 0.0106, 0.0123, 0.0142, 0.0165, 0.0190, 0.0220, 0.0255,0.0295, 0.0341, 0.0395, 0.0457, 0.0529, 0.0612, 0.0708, 0.0819},
	/*2--PolyPN*/{0.0, 0.00023, 0.00028, 0.00032, 0.00035, 0.00037, 0.00039, 0.00041,0.00043, 0.00044, 0.00046, 0.00047, 0.00049, 0.00050, 0.00051, 0.00052},
	/*3--PolyTR*/ /*{0.0000, 0.0004, 0.0008, 0.0012, 0.0017, 0.0021, 0.0025, 0.0029,0.0033, 0.0037, 0.0042, 0.0046, 0.005, 0.0054, 0.0058, 0.0062},*/
	{0.0,0.00277,0.00333,0.00388,0.00444,0.00500,0.00555,0.00556,0.00557,0.00558,0.00559,0.00666,0.00722,0.00999,0.0116,0.01944},//Yupi0715
	/*4--Wool*/  {0.0000, 0.0325, 0.0500, 0.0650, 0.0738, 0.0850, 0.0930, 0.1050,0.1150, 0.1320, 0.1500, 0.1660, 0.1870, 0.2100, 0.2530, 0.3800}};
	
	/* randa(lg)*/
	
	static final double FIBERPROPERTY[][]={
			/*fiber name      radius0   Density1 ThermoConductivity2   VapDiffusion3  LqdContactAngle4  Emissivity5 heatSLqd6
			/*0--Cotton*/    {2.06e-3,    1.550,    7.20e-4,             6.00e-9,       80,           0.9,     2260.0},
			/*1--Nylon*/     {2.04e-3,    0.935,    27.5e-4,             3.90e-9,       70,           0.9,     2260.0},
			/*2--PolyPn*/    {2.00e-3,    0.910,    25.0e-4,             1.30e-9,       70,           0.9,     2260.0},
			/*3--PolyTR*/    {2.00e-3,    1.380,    8.40e-4,             3.90e-9,       70,           0.9,     2260.0},
			/*4--Wool*/      {2.06e-3,    1.320,    5.20e-4,             9.00e-9,       80,           0.9,     2260.0},
			/*5--concrete*/  {2.04e-3,    0.696,    1.28e-2,             5.80e-9,       80,           0.9,     2260.0}
			};
	
	ModelSelection m_modelSelected = new ModelSelection();
	ArrayList<FiberType> m_fiberList = new ArrayList<>();
	ArrayList<MembraneType> m_membraneList = new ArrayList<>();
	ArrayList<PCMType> m_pcmList = new ArrayList<>();
	ArrayList<FabricType> m_fabricList = new ArrayList<>();
	ArrayList<GarmentType> m_garmentList = new ArrayList<>();
	ArrayList<GarmentType> m_oringinalGarmentList = new ArrayList<>();
	ArrayList<GarmentParts> m_garmentRecords = new ArrayList<>();
	ArrayList<StageType> m_stageList = new ArrayList<>();
	ArrayList<GarmentType> m_garmentPart = new ArrayList<>();
	ControlType m_controls = new ControlType();
	int m_bodyGarment[] = new int[6];//记录衣物的覆盖情况
	char m_ClothingIdList[][] = new char[100][200];
	int  m_ClothNum;
	
	//衣物相关
	ArrayList<TNumericalGarment> m_numerialGarmentList = new ArrayList<>();
	GarmentRecord m_withGarment = new GarmentRecord();
	ArrayList<TNumericalGarment> m_bodyGarmentList[] = new ArrayList[6];
	
	//人体模型相关
	BodyType m_body = new BodyType();
	BodyType m_pBody = new BodyType();
	T25NodeBody m_humanbody = new T25NodeBody();
	int m_bodySegment;
	int m_bodyLayer;
	int modelFlag;
	ArrayList<BoundarySeg> m_boundarySeg[] = new ArrayList[6];
	String m_fileDir;
	boolean m_bSimulating;
	
	//数据库存储相关
	JsonArray records;

	//从xml中加载默认配置
	boolean LoadXML(String filePath) throws Exception {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.parse(filePath);
		//NodeList list;
		
		//热湿模型的选项设置
		Element SimulationModel = (Element)doc.getFirstChild();
		m_modelSelected.localTemperature = SimulationModel.getAttribute("localTemperatrue").equals("1") ? true : false;
		m_modelSelected.nonLocalTemperature = SimulationModel.getAttribute("nonLocalTemperature").equals("1") ? true : false;
		m_modelSelected.liquid = SimulationModel.getAttribute("liquid").equals("1") ? true : false;
		m_modelSelected.wateVapor = SimulationModel.getAttribute("wateVapor").equals("1") ? true : false;
		m_modelSelected.pressure = SimulationModel.getAttribute("pressure").equals("1") ? true : false;
		m_modelSelected.virus = SimulationModel.getAttribute("virus").equals("1") ? true : false;
		m_modelSelected.membrane = SimulationModel.getAttribute("membrane").equals("1") ? true : false;
		m_modelSelected.PCM = SimulationModel.getAttribute("PCM").equals("1") ? true : false;
		m_modelSelected.selfHeating = SimulationModel.getAttribute("selfHeating").equals("1") ? true : false;
		m_modelSelected.radiation = SimulationModel.getAttribute("radiation").equals("1") ? true : false;
		m_modelSelected.bodyModel = SimulationModel.getAttribute("bodyModel").equals("1") ? true : false;
		m_modelSelected.babyModel = SimulationModel.getAttribute("babyModel").equals("1") ? true : false;
		m_modelSelected.comfort = SimulationModel.getAttribute("comfort").equals("1") ? true : false;
		
		//人体默认设置
		Element body = (Element)doc.getElementsByTagName("Body").item(0);
		m_pBody.m_modelId = body.getAttribute("ModelId");
		//---这三个参数应根据用户输入调整---
		m_pBody.m_gender = Integer.valueOf(body.getAttribute("Gender"));
		m_pBody.m_sizeClass = body.getAttribute("SizeClass");
		m_pBody.m_size = body.getAttribute("Size");
		//-----------------------------
		m_pBody.m_nation = body.getAttribute("Nation");
		
		/*Element control = (Element)doc.getElementsByTagName("Control").item(0);
		m_controls.TimeStep = Double.valueOf(control.getAttribute("TimeStep"));
		m_controls.SaveFrequency = Double.valueOf(control.getAttribute("SaveFrequency"));
		m_controls.FabricGrid = Double.valueOf(control.getAttribute("FabricGrid"));
		m_controls.FiberGrid = Double.valueOf(control.getAttribute("FiberGrid"));
		m_controls.InitialT = Double.valueOf(control.getAttribute("InitialT"));
		m_controls.InitialRH = Double.valueOf(control.getAttribute("InitialRH"));
		m_controls.InitialP = Double.valueOf(control.getAttribute("InitialP"));
		m_controls.InitialWater = Double.valueOf(control.getAttribute("InitialWater"));
		m_controls.VirusInitial = Double.valueOf(control.getAttribute("VirusInitial"));
		m_fileDir = control.getAttribute("SavePath");*/
		
		/*for (int i = 0; i<6; i++)
		{
			m_boundarySeg[i].clear();
		}*/
		
		for (int i = 0; i<m_stageList.size(); ++i)
		{
			StageType theIterator = m_stageList.get(i);
			for (int loop = 0; loop<6; ++loop)
				SetBoundarySegmentCondition(theIterator, loop);
		}
		return true;
	};

	SimulateData() {
		m_bodySegment=6;

		m_controls.TimeStep = 0.01;
		m_controls.SaveFrequency = 1;
		m_controls.FabricGrid = 5;
		m_controls.FiberGrid = 8;
		m_controls.InitialT = 293.16;
		m_controls.InitialRH = 0.65;
		m_controls.InitialWater = 0.001;
		m_controls.InitialP = 101325;
		m_controls.VirusInitial = 1E-10;

		SetModelSelectionParameters();
		SetBodyParameters();//设置人体参数，实际上可以用静态成员变量取代

		m_ClothNum = 0;

		m_bSimulating = false;
		//data_coming_flag = false;
		core_temp_flag = false;
		
		for(int i=0; i<6; i++) {
			m_bodyGarmentList[i] = new ArrayList<TNumericalGarment>();
			m_boundarySeg[i] = new ArrayList<BoundarySeg>();
		}
		
		records = new JsonArray();
	};

	int HumanSimulation() throws Exception{
		if (m_bSimulating)
		{
			return 0;
		}
		m_bSimulating = true;

		BufferedWriter bodyData;
		BufferedWriter bodySegData[] = new BufferedWriter[6];

		//fiberFileIndex似乎没什么用
		//int[] fiberFileIndex = new int[6];
		/*for (int i = 0; i<6; ++i)
		{
			fiberFileIndex[i] = 0;
		}*/

		String[] FileAppendix = new String[6];
		double time, timeCounter, stageTime;
		double spacePosition, timePosition;
		StageType stage;
		TNumericalGarment garment;

		int[] fabricTime = new int[m_bodySegment];
		double[] tClothingStore = new double[m_bodySegment];
		double[] tClothing = new double[m_bodySegment];
		double[] rhClothingStore = new double[m_bodySegment];
		double[] rhClothing = new double[m_bodySegment];

		if (m_modelSelected.bodyModel)
		{
			FileAppendix[0] = new String("Head");
			FileAppendix[1] = new String("Trunk");
			FileAppendix[2] = new String("Arm");
			FileAppendix[3] = new String("Hand");
			FileAppendix[4] = new String("Leg");
			FileAppendix[5] = new String("Foot");
		}
		else if (m_modelSelected.babyModel)
		{
			FileAppendix[0] = new String("Head");
			FileAppendix[1] = new String("Trunk");
			FileAppendix[2] = new String("Arm");
			FileAppendix[3] = new String("Leg");;
		}

		DefineMaterial();
		String m_strResultFileDir = m_fileDir;
		String path1;
		File file;
		//fiberNum似乎没什么用
		//int fiberNum = 0;

		for (int k = 0; k<m_bodySegment; k++)
		{
			if (m_withGarment.PartsWithGarment[k]){
				garment = (TNumericalGarment)m_bodyGarmentList[k].get(0);
				/*for (int t = 0; t<garment.m_fabricList.size(); t++)
				{
					TNumericalFabric fabric = garment.m_fabricList.get(t);
					for (int j = 0; j<fabric.m_includedFibers.size(); j++)
					{
						//TNumericalFiber fiber = fabric.m_includedFibers.get(j);
						//String filePath = String.format("%s\\fiber%s%d%s.dat", m_strResultFileDir, FileAppendix[k], t, fiber.m_fiberType);
						fiberNum++;
					}
				}
				fiberFileIndex[k] = fiberNum;*/
			}
		}

		path1 = m_strResultFileDir + File.separator + "bodyProperty.dat";
		file = new File(path1);
		if(!file.exists()) file.createNewFile();
		bodyData = new BufferedWriter(new FileWriter(file));

		path1 = m_strResultFileDir + File.separator + "SkinTemp.dat";
		file = new File(path1);
		if(!file.exists()) file.createNewFile();
		BufferedWriter Test = new BufferedWriter(new FileWriter(file));

		path1 = m_strResultFileDir + File.separator + "SweatRate.dat";
		file = new File(path1);
		if(!file.exists()) file.createNewFile();
		BufferedWriter Test2 = new BufferedWriter(new FileWriter(file));

		path1 = m_strResultFileDir + File.separator + "headProperty.dat";
		file = new File(path1);
		if(!file.exists()) file.createNewFile();
		bodySegData[0] = new BufferedWriter(new FileWriter(file));

		path1 = m_strResultFileDir + File.separator + "trunkProperty.dat";
		file = new File(path1);
		if(!file.exists()) file.createNewFile();
		bodySegData[1] = new BufferedWriter(new FileWriter(file));

		path1 = m_strResultFileDir + File.separator + "armProperty.dat";
		file = new File(path1);
		if(!file.exists()) file.createNewFile();
		bodySegData[2] = new BufferedWriter(new FileWriter(file));

		path1 = m_strResultFileDir + File.separator + "handProperty.dat";
		file = new File(path1);
		if(!file.exists()) file.createNewFile();
		bodySegData[3] = new BufferedWriter(new FileWriter(file));

		path1 = m_strResultFileDir + File.separator + "legProperty.dat";
		file = new File(path1);
		if(!file.exists()) file.createNewFile();
		bodySegData[4] = new BufferedWriter(new FileWriter(file));

		path1 = m_strResultFileDir + File.separator + "footProperty.dat";
		file = new File(path1);
		if(!file.exists()) file.createNewFile();
		bodySegData[5] = new BufferedWriter(new FileWriter(file));
		
		//int frequency = (int)(m_controls.SaveFrequency / m_controls.TimeStep);	
		//fiberNum = 0;

		/*for (int t = 0; t<m_bodySegment; t++)
		{
			if (m_withGarment.PartsWithGarment[t])
			{
				if (m_bodyGarmentList[t].get(0) != null)
				{
					//取得身体部位上覆盖的第一件衣物，默认同一部位上的衣物都是相同的，所以取第一件就好了
					garment = (TNumericalGarment)m_bodyGarmentList[t].get(0);
					for (int k = 0; k<garment.m_fabricList.size(); k++)
					{
						//TNumericalFabric fabric = (TNumericalFabric)garment.m_fabricList.get(k);
						// the time column
						if (k == 0)
						{
							spacePosition = 0.0;
							timePosition = 0.0;
						}			
					}
				}
			}
		}*/

		time = 0;
		timeCounter = 0;
		stageTime = 0;
		for (int i = 0; i<m_bodySegment; i++)
		{
			fabricTime[i] = 0;
		}	

		for (int sn = 0; sn<m_stageList.size(); sn++)
		{
			SetStage(sn);
			stage = (StageType)m_stageList.get(sn);
			stageTime += stage.Time * 6;//如果用户输入时长是20min（即1200秒），这里会变成20/100*6=1.2
			
			for (int t = 0; t<m_bodySegment; t++)
			{
				m_humanbody.m_work = m_boundarySeg[t].get(sn).m_BHuman.m_metabolicRate;//代谢率
				stage.InTemperature = m_boundarySeg[t].get(sn).m_BEnv.m_temperature;
				stage.InRH = m_boundarySeg[t].get(sn).m_BEnv.m_humidity;
				stage.InWater = m_boundarySeg[t].get(sn).m_BEnv.m_liquidFraction;

				if (stage.BodyGarment[t]<0)//没有衣物覆盖的身体部位的边界温湿度（等于外界环境温湿度）
				{
					tClothing[t] = stage.OutTemperatrue;
					rhClothing[t] = stage.OutRH;				
				}
				else
				{
					garment = (TNumericalGarment)m_bodyGarmentList[t].get(sn);
					TNumericalFabric fabric = (TNumericalFabric)garment.m_fabricList.get(0);//获取衣服的最外层织物
					tClothingStore[t] = fabric.m_tFabric[0].preNodeValue;
					rhClothingStore[t] = fabric.m_RH[0];
				}
			}
			//int counter_flag = 0;
			//boolean bad_flag = false;
			
			do
			{
				/*while (true)
				{
					if (data_coming_flag)
					{
						counter_flag = 0;
						break;					
					}
					else{
						counter_flag++;
						Thread.sleep(500);
						//Sleep(500);
					}
					if (counter_flag > 41){
						bad_flag = true;
						break;
					}
				}
				if (bad_flag){
					break;
				}
				data_coming_flag = false;*/
				Test2.write("current MetabolicRate:\t");
				Test2.write(String.valueOf(m_humanbody.m_work));
				Test2.write("\t\t");
				for (int t = 0; t<m_bodySegment; t++)
				{
					if (stage.BodyGarment[t]>-1)
					{
						tClothing[t] = tClothingStore[t];
						rhClothing[t] = rhClothingStore[t];
					}
				}
				//fiberNum = 0;
				//遍历各身体部位上的衣物
				for (int t = 0; t<m_bodySegment; t++)
				{
					if (stage.BodyGarment[t]>-1)
					{
						garment = (TNumericalGarment)m_bodyGarmentList[t].get(sn);//取出身体部位上对应当前运动阶段的衣服
						for (int i = 0; i <= 10 * 1 / m_controls.TimeStep; i++) //10s是衣物和身体进行数据交换的频率
						{
							spacePosition = 0.0;
							for (int k = 0; k<garment.m_fabricList.size(); k++)
							{
								TNumericalFabric fabric = (TNumericalFabric)garment.m_fabricList.get(k);
								//设置织物的参数，在下一步计算矩阵初值时会用到这些参数
								fabric.EquationsCoefficient(garment.m_gas, garment.m_liquid, garment.m_virus, i, true);
								//为左右矩阵赋初始值
								garment.m_equationSolver.EquationsMatrix(fabric, garment.m_gas, garment.m_liquid, garment.m_virus, stage.RadiationConstant, garment.m_selfHeat);

								if (m_modelSelected.bodyModel)//正常情况下都会选中bodyModel
								{
									garment.m_equationSolver.GetMatrixBoundarytyty(fabric, garment.m_gas, garment.m_liquid, garment.m_virus, m_humanbody.m_eSkin[t], m_modelSelected.bodyModel, garment.m_selfHeat, t, m_boundarySeg[t].get(sn).m_Bfabric.get(k).m_iMassPumpingRation, m_boundarySeg[t].get(sn).m_Bfabric.get(k).m_iHeatPumpingRation, m_humanbody.m_totalHeatEvaporation);
									if (m_modelSelected.babyModel)
									{
										garment.m_equationSolver.GetMatrixBoundary(fabric, garment.m_gas, garment.m_liquid, garment.m_virus, 0, m_modelSelected.bodyModel, garment.m_selfHeat);
									}
									garment.m_equationSolver.SolveEquations();//解矩阵等式

									//定时记录矩阵信息
									if (t == 1 && k == 0 && fabricTime[t] % (int)(m_controls.SaveFrequency / m_controls.TimeStep * 60) == 0)
									{
										int s_mNum = 0;//s_mNum原本设计为多线程同步计数，不过现在已经没用了

										path1 = m_strResultFileDir + File.separator + "matrix" + s_mNum + ".dat";
										BufferedWriter matricfile = new BufferedWriter(new FileWriter(path1.toString()));

										timePosition = (fabricTime[t] + 1) / 60.0*m_controls.TimeStep;

										for (int l1 = 0; l1<2 * fabric.m_nx; ++l1)
										{
											for (int l2 = 0; l2<2 * fabric.m_nx; ++l2)
											{
												matricfile.write(String.valueOf(garment.m_equationSolver.m_matrixLeft[l1][l2]));
												matricfile.write("\t");
											}
											matricfile.write("\t\t|\t");
											matricfile.write(String.valueOf(garment.m_equationSolver.m_matrixRight[l1]));
											matricfile.write("\n");
										}
										matricfile.close();
										s_mNum++;
									}
								}

								//更新织物的属性（包括里面的fiber的属性），部分计算需要用到前面计算后的左右矩阵
								garment.m_equationSolver.UpdateCoefficientValue(fabric, garment.m_gas, garment.m_liquid, garment.m_virus);
								garment.m_equationSolver.ClearMatrix();
								UpdateBoundaryCondition(k, garment.m_fabricList);
								if (k == 0)
								{
									++fabricTime[t];
								}

								if (fabricTime[t] % (int)(m_controls.SaveFrequency / m_controls.TimeStep) == 0)
								{
									timePosition = (fabricTime[t] + 1) / 60.0*m_controls.TimeStep;

									/*for (int j = 0; j<fabric.m_includedFibers.size(); j++)
									{
										//TNumericalFiber fiber = fabric.m_includedFibers.get(j);									
										fiberNum++;
										if (fiberNum == fiberFileIndex[t])
										{
											if (t == 0)
											{
												fiberNum = 0;
											}
											else
											{
												fiberNum = fiberFileIndex[t - 1];
											}
										}
									}*/

									for (int j = 0; j<fabric.m_nx; j++)
									{
										spacePosition = spacePosition + (k + j)*fabric.m_deltaX;													
									}								
								}
							}//fabricList遍历结束
						}//交换频率结束

						TNumericalFabric fabric = (TNumericalFabric)garment.m_fabricList.get(0);//获取第一层织物
						tClothingStore[t] = fabric.m_tFabric[0].preNodeValue;
						rhClothingStore[t] = fabric.m_RH[0];
					}//if(stage.BodyGarment[t]>-1)结束
				}//身体部位遍历结束

				if (m_modelSelected.bodyModel)
				{
					//人体热调节计算。参数time恒定为0,不知道有什么用。热调节的作用是更新皮肤/内核温度和湿度
					m_humanbody.ThermalRegulation(tClothing, rhClothing, stage.OutTemperatrue, stage.OutRH, time, (int)timeCounter, m_garmentRecords.get(0).coverRatio);
					if (true) timePosition = timeCounter * 10 / 60;
				}

				SetBoundaryTemperature(stage, sn);//更新织物的边界温度

				timeCounter++;//timeCounter每加1相当于加100秒

				double warmness[] = new double[6];
				double dampness[] = new double[6];
				double comfort[] = new double[6];
				for (int i = 0; i<m_bodySegment; i++)
				{
					warmness[i] = 464.44*Math.log(m_humanbody.m_tSkin[i] - 273.16) - 1602.8;//注意这里是用皮肤温度计算
					dampness[i] = 0.0804*Math.sqrt(m_humanbody.m_rh[i]) - 7.9802*(m_humanbody.m_rh[i]) + 209.36;
					comfort[i] = (warmness[i] + dampness[i]) / 2.0;//舒适度
				}
		
				Test.write(String.valueOf(timeCounter * 10 / 60));
				Test.write('\t');

				JsonObject record = new JsonObject();
				record.addProperty("time", timeCounter * 10 / 60);
				JsonArray temps = new JsonArray();
				JsonArray sweats = new JsonArray();
				for (int i = 0; i<m_bodySegment; i++)
				{
					//---这里将温度和湿度保存到仿真记录中，用于返回给用户---
					temps.add(m_humanbody.m_tSkin[i] - 273.16);
					sweats.add(m_humanbody.m_sweatRate[i] * 100);
					//---------------------------------------------
					bodyData.write(String.valueOf(timeCounter * 10 / 60));
					bodyData.write("\t"+i+"\t");
					bodyData.write(String.valueOf(m_humanbody.m_tSkin[i] - 273.16)); bodyData.write("\t");
					bodyData.write(String.valueOf(m_humanbody.m_tCore[i])); bodyData.write("\t");
					bodyData.write(String.valueOf(m_humanbody.m_totalHeatMetabolic[m_bodyLayer*i + 3])); bodyData.write("\t");
					bodyData.write(String.valueOf(m_humanbody.m_rh[i] * 100)); bodyData.write("\t");
					bodyData.write(String.valueOf(m_humanbody.m_totalBloodFlow[i])); bodyData.write("\t");
					bodyData.write(String.valueOf(m_humanbody.m_sweatRate[i] * 100)); bodyData.write("\t");
					bodyData.write(String.valueOf(m_humanbody.m_sAccumulatedSkin[i])); bodyData.write("\t");
					bodyData.write(String.valueOf(m_humanbody.m_totalHeatEvaporation[m_bodyLayer*i + 3])); bodyData.write("\t");
					bodyData.write(String.valueOf(m_humanbody.m_pSkin[i]));
					
					Test.write(String.valueOf(m_humanbody.m_tSkin[i] - 273.16));
					Test.write("\t");

					bodySegData[i].write(String.valueOf(timeCounter * 10 / 60)); bodySegData[i].write("\t");
					bodySegData[i].write(String.valueOf(m_humanbody.m_tSkin[i] - 273.16)); bodySegData[i].write("\t");
					bodySegData[i].write(String.valueOf(m_humanbody.m_tCore[i])); bodySegData[i].write("\t");
					bodySegData[i].write(String.valueOf(m_humanbody.m_totalHeatMetabolic[m_bodyLayer*i + 3])); bodySegData[i].write("\t");
					bodySegData[i].write(String.valueOf(m_humanbody.m_rh[i] * 100)); bodySegData[i].write("\t");
					bodySegData[i].write(String.valueOf(m_humanbody.m_heatFlowRate[i])); bodySegData[i].write("\t");
					bodySegData[i].write(String.valueOf(m_humanbody.m_sweatRate[i] * 100)); bodySegData[i].write("\t");
					bodySegData[i].write(String.valueOf(m_humanbody.m_sAccumulatedSkin[i])); bodySegData[i].write("\t");
					bodySegData[i].write(String.valueOf(m_humanbody.m_totalHeatEvaporation[m_bodyLayer*i + 3])); bodySegData[i].write("\t");
					bodySegData[i].write(String.valueOf(m_humanbody.m_pSkin[i]));
								
					bodyData.newLine();
					bodySegData[i].newLine();
				}
				//---这里将温度和湿度保存到仿真记录中，用于返回给用户---
				record.add("temperature", temps);
				record.add("sweatrate", sweats);
				records.add(record);
				//----------------------------------------------
				Test2.write("Core Temperature: ");
				Test2.write(String.valueOf(m_humanbody.m_tCore[1]));
				Test2.newLine();;
					
				double sweataccu = m_humanbody.m_sAccumulatedSkin[0] + m_humanbody.m_sAccumulatedSkin[1] + m_humanbody.m_sAccumulatedSkin[2] * 2 + m_humanbody.m_sAccumulatedSkin[3] * 2 + m_humanbody.m_sAccumulatedSkin[4] * 2 + m_humanbody.m_sAccumulatedSkin[5] * 2;
				if (sweataccu < 0.01) sweataccu = 0.0;
				m_humanbody.m_sAccumulatedSkin[1] = sweataccu;
				core_temp_flag = true;
			} while (timeCounter - stageTime < 0);
		}//stageList遍历结束

		bodyData.close();
		Test.close();
		Test2.close();

		//fiberNum = 0;
		for (int k = 0; k<m_bodySegment; k++)
		{
			bodySegData[k].close();
			if (m_withGarment.PartsWithGarment[k])
			{
				if (m_bodyGarmentList[k].size()<1) break;
				garment = (TNumericalGarment)m_bodyGarmentList[k].get(0);
				/*for (int t = 0; t<garment.m_fabricList.size(); t++)
				{
					TNumericalFabric fabric = (TNumericalFabric)garment.m_fabricList.get(t);
					for (int j = 0; j<fabric.m_includedFibers.size(); j++)
					{
						fiberNum++;
					}
				}*/
			}
		}
		m_bSimulating = false;

		return 1;
	};

	void DefaultFiber() {
		for(int k=0;k<5;k++)
		{
			FiberType fiberProperty = new FiberType();
			fiberProperty.FiberName = FIBERNAME[k];
			fiberProperty.Density = FIBERPROPERTY[k][1]; /* rho(f)*/
			fiberProperty.Diameter = FIBERPROPERTY[k][0]; /* Rf*/
			fiberProperty.VaporDiff = FIBERPROPERTY[k][3];
			fiberProperty.Emissivity = FIBERPROPERTY[k][5];
			fiberProperty.LiqConAngle = FIBERPROPERTY[k][4];
			for(int i=0;i<16;i++)
			{
				fiberProperty.FiberRegainY[i] = RH_REGAIN_Y[k][i];
				fiberProperty.FiberRegainX[i] = i/15.0;
			}
			m_fiberList.add(fiberProperty);
		}
	};
	
	void DefineMaterial() {
		GarmentType garment;
		StageType stage,stageTemp;
		boolean IsCorrect,IsEqual;

		//这种情况可以忽略，默认是选择了bodyModel
		if(!m_modelSelected.bodyModel && !m_modelSelected.babyModel)
		{
			m_numerialGarmentList.clear();
			for(int i=0;i<m_garmentList.size();i++)
			{
				garment=(GarmentType)m_garmentList.get(i);
				DefineNumericalGarment(garment,m_numerialGarmentList);
			}
		}
		else
		{
			if(m_modelSelected.bodyModel)
			{
				//---传入m_body以设置25节点人体特征，实际上可以用静态变量---
				m_humanbody.InitialValue(m_body);
				//---------------------------------------------
				m_bodySegment=6;
				m_bodyLayer=4;
			}

			for(int i=0;i<m_bodySegment;i++)
			{
				m_bodyGarmentList[i].clear();
				m_withGarment.PartsWithGarment[i]=false;
				m_withGarment.FirstPosition[i]=-1;
			}

			for(int i=0;i<m_stageList.size();i++)
			{
				stage=(StageType)m_stageList.get(i);
				for(int j=0;j<m_bodySegment;j++)
				{
					if(stage.BodyGarment[j]>-1)//首先判断该部位是否有衣物覆盖
					{
						//判断各部位的服装在不同运动阶段是否相同，正常情况下应该是相同的
						IsEqual=false;
						for(int k=0;k<i;k++)
						{
							stageTemp=(StageType)m_stageList.get(k);
							if(stage.BodyGarment[j]==stageTemp.BodyGarment[j])
							{
								m_bodyGarmentList[j].add(m_bodyGarmentList[j].get(k));
								IsEqual=true;
							}
						}
						if(!IsEqual)
						{
							garment=(GarmentType)m_garmentList.get(stage.BodyGarment[j]);
							DefineNumericalGarment(garment,m_bodyGarmentList[j]);
							m_withGarment.PartsWithGarment[j]=true;
							if(m_withGarment.FirstPosition[j]==-1)
							{
								m_withGarment.FirstPosition[j]=i;
							}
						}
					}
					else
					{
						m_bodyGarmentList[j].add(null);
					}
				}
			}
		}
	};

	void DefineNumericalGarment(GarmentType garment,ArrayList<TNumericalGarment> garmentList) {
		ArrayList<TNumericalFiber> numericalfiberList = new ArrayList<>();
		FabricType fabric;
		FiberType fiber;
		TNumericalGarment numericalGarment;
		TNumericalFabric numericalFabric;
		int index;

		numericalGarment = new TNumericalGarment();
		numericalGarment.m_gas = new TNumericalGas((int)m_controls.FabricGrid);
		numericalGarment.m_gas.InitialValue(garment.Gas);

		numericalGarment.m_liquid = new TNumericalLiquidWater();
		numericalGarment.m_liquid.InitialValue(garment.Liquid);

		for(int j=0;j<garment.FabricList.size();j++)
		{
			index=(int)(garment.FabricList.get(j));
			fabric=(FabricType)m_fabricList.get(index);
			for(int k=0;k<fabric.FiberList.size();k++)
			{
				fiber=(FiberType)fabric.FiberList.get(k);
				TNumericalFiber numericalFiber = new TNumericalFiber((int)m_controls.FiberGrid,fabric.MassRatio.get(k));
				numericalFiber.InitialValue(fiber);
				numericalfiberList.add(numericalFiber);
			}

			if(fabric.PCM != null)
			{
				garment.sourceItem.PCM = true;
			}
			else
			{
				garment.sourceItem.PCM = false;
			}

			numericalFabric = new TNumericalFabric((int)m_controls.FabricGrid,(int)m_controls.FiberGrid,numericalfiberList,garment.modelFlag,garment.sourceItem,j);
			numericalFabric.InitialValue(numericalGarment.m_gas,numericalGarment.m_liquid,fabric,m_controls);
			if(fabric.PCM != null)
			{
				numericalFabric.m_pcm = new TNumericalPCM();
				numericalFabric.m_pcm.InitialValue(fabric.PCM);
			}
			numericalFabric.m_includedMembranes[0] = fabric.InnerMembrane;
			numericalFabric.m_includedMembranes[1] = fabric.OuterMembrane;

			numericalGarment.m_fabricList.add(numericalFabric);
		}//end for j

		if(garment.modelFlag.compareTo(ModelType.AddVirus)>=0)
		{
			numericalGarment.m_virus = new TNumericalVirus();
			numericalGarment.m_virus.InitialValue(garment.Virus);
		}
		if(garment.sourceItem.SelfHeating)
		{
			numericalGarment.m_selfHeat = new TNumericalSelfHeating();
			numericalGarment.m_selfHeat.InitialValue(garment.SelfHeat);
		}
		numericalGarment.m_equationSolver = new TEquationsSolver((int)m_controls.FabricGrid,garment.modelFlag);
		garmentList.add(numericalGarment);
	};
	
	void SetStage(int stageNumber) {
		//TNumericalFabric fabric;
		StageType stage;
		double airThickness=10.0;

		stage=(StageType)m_stageList.get(stageNumber);

		SetTransferCoefficient(stage,stageNumber);

		if(m_modelSelected.bodyModel)
		{
			SetBoundaryCondition(m_modelSelected,stage,stageNumber,m_humanbody.m_cWaterVapor,m_humanbody.m_tSkin,m_humanbody.m_rh);
			// work的值必须大于86.5
			if(stage.Work-86.5<=0) m_humanbody.m_work=0.0;
			else m_humanbody.m_work=stage.Work-86.5;

			for(int i = 0;i <m_bodySegment;i++)
			{
				//分两种情况讨论：没衣服覆盖和有衣服
				if (stage.BodyGarment[i]<0)
				{
					m_humanbody.m_heatTransferCoe[i] =(4.9+3.43+5.93*stage.OutVelocity)*m_humanbody.m_surface[i];
					m_humanbody.m_heatResAir[i] =66.8/(3.43+5.93*stage.OutVelocity);
				}
				else
				{
					m_humanbody.m_heatTransferCoe[i] = (4.9+24.0/airThickness)*m_humanbody.m_surface[i];
					m_humanbody.m_heatResAir[i] = 2.2*airThickness;
				}
			}
		}
		else
		{
			SetBoundaryCondition(m_modelSelected,stage,stageNumber,null,null,null);
		}
	};

	void SetTransferCoefficient(StageType stage,int stageNumber) {
		TNumericalGarment garment;
		TNumericalFabric fabric;

		if(m_modelSelected.bodyModel || m_modelSelected.babyModel)
		{
			for(int i=0;i<m_bodySegment;i++)
			{
				if(stage.BodyGarment[i]>-1)
				{
					garment=(TNumericalGarment)m_bodyGarmentList[i].get(stageNumber);

					for(int k=0;k<garment.m_fabricList.size();k++)
					{
						fabric=(TNumericalFabric)garment.m_fabricList.get(k);
						fabric.BoundaryStatus(garment.m_gas,garment.m_fabricList.size(),stage,i,k,m_boundarySeg[i].get(stageNumber).m_Bfabric.get(k));
					}
				}
			}
		}
		else
		{
			for(int i=0;i<m_numerialGarmentList.size();i++)
			{

				garment=(TNumericalGarment)m_numerialGarmentList.get(i);

				for(int k=0;k<garment.m_fabricList.size();k++)
				{
					fabric=(TNumericalFabric)garment.m_fabricList.get(k);
				}  
			}
		}
	};

	void SetBoundaryCondition(ModelSelection modelSelected,StageType stage,int stageNumber,double[] cVapor,double[] tSkin, double[] rhSkin) {
		TNumericalFabric fabric;
		TNumericalGarment garment;
		int layCount=0;

		if(m_modelSelected.bodyModel ||m_modelSelected.babyModel)
		{
			for(int k=0;k<m_bodySegment;k++)
			{
				if(stage.BodyGarment[k]>-1)
				{
					garment=(TNumericalGarment)m_bodyGarmentList[k].get(stageNumber);
					for(int i=0;i<garment.m_fabricList.size();i++)
					{
						fabric=(TNumericalFabric)garment.m_fabricList.get(i);
						//20+273.16;
						fabric.m_temperatureInner=stage.OutTemperatrue
							+(tSkin[k]-stage.OutTemperatrue)/garment.m_fabricList.size()
							*(garment.m_fabricList.size()-i);
						//25+273.16;
						fabric.m_temperatureOuter=tSkin[k]
						+(stage.OutTemperatrue-tSkin[k])/garment.m_fabricList.size()
							*(i+1);
						//0.6;
						fabric.m_RHInner=stage.OutRH
							+(rhSkin[k]-stage.OutRH)/garment.m_fabricList.size()
							*(garment.m_fabricList.size()-i);
						//0.6;
						fabric.m_RHOuter=rhSkin[k]
						+(stage.OutRH-rhSkin[k])/garment.m_fabricList.size()
							*(i+1);

						if(i==0 && stageNumber==0)
						{
							fabric.m_CVaporInner=TPhysicalPro.WaterVaporConcentration(fabric.m_temperatureInner)* fabric.m_RHInner;
						}
						else if(i==0)
						{
							fabric.m_CVaporInner=cVapor[k];
						}

						if(fabric.m_modelFlag.compareTo(ModelType.AddLiquid)>=0)
						{                          //
							fabric.m_vLiquidInner=stage.OutWater
								+(stage.InWater - stage.OutWater)/garment.m_fabricList.size()
								*(garment.m_fabricList.size()-i);
							//
							fabric.m_vLiquidOuter=stage.InWater
								+(stage.OutWater-stage.InWater)/garment.m_fabricList.size()
								*(i+1);
						}

						if(fabric.m_modelFlag.compareTo(ModelType.AddPressure)>=0)
						{                           //1013250;
							fabric.m_pressureInner=stage.OutPressure
								+(stage.InPressure- stage.OutPressure)/garment.m_fabricList.size()
								*(garment.m_fabricList.size()-i);
							//1013250;
							fabric.m_pressureOuter=stage.InPressure
								+(stage.OutPressure-stage.InPressure)/garment.m_fabricList.size()
								*(i+1);
						}
					}//end for
				}//end if
			}//end for
		}//end if
		else
		{
			for(int k=0;k<m_numerialGarmentList.size();k++)
			{
				garment=(TNumericalGarment)m_numerialGarmentList.get(k);
				layCount+=garment.m_fabricList.size();
			}

			for(int k=0;k<m_numerialGarmentList.size();k++)
			{
				garment=(TNumericalGarment)m_numerialGarmentList.get(k);
				for(int i=0;i<garment.m_fabricList.size();i++)
				{
					fabric=(TNumericalFabric)garment.m_fabricList.get(i);
					fabric.m_temperatureInner=stage.OutTemperatrue
						+(stage.InTemperature-stage.OutTemperatrue)/layCount
						*(layCount-i);
					fabric.m_temperatureOuter=stage.InTemperature
						+(stage.OutTemperatrue-stage.InTemperature)/layCount
						*(i+1);
					fabric.m_RHInner=stage.OutRH
						+(stage.InRH-stage.OutRH)/layCount
						*(layCount-i);
					fabric.m_RHOuter=stage.InRH
						+(stage.OutRH-stage.InRH)/layCount
						*(i+1);

					if(fabric.m_modelFlag.compareTo(ModelType.AddLiquid)>=0)
					{                          // 0.001;
						fabric.m_vLiquidInner=stage.OutWater
							+(stage.InWater - stage.OutWater)/garment.m_fabricList.size()
							*(garment.m_fabricList.size()-i);
						// 0.001;
						fabric.m_vLiquidOuter=stage.InWater
							+(stage.OutWater-stage.InWater)/garment.m_fabricList.size()
							*(i+1);
					}

					if(fabric.m_modelFlag.compareTo(ModelType.AddPressure)>=0)
					{                           //1013250;
						fabric.m_pressureInner=stage.OutPressure
							+(stage.InPressure- stage.OutPressure)/garment.m_fabricList.size()
							*(garment.m_fabricList.size()-i);
						//1013250;
						fabric.m_pressureOuter=stage.InPressure
							+(stage.OutPressure-stage.InPressure)/garment.m_fabricList.size()
							*(i+1);
					}
				}//end for i
			}//end for K
		}//end else 
	};
	
	void UpdateBoundaryCondition(int layNumber,Vector<TNumericalFabric> fabricList) {
		TNumericalFabric fabric,fabricNext,fabricPre;

		for(int k=0;k<m_bodySegment;k++)
		{
			if(fabricList.size()<2)//如果衣服只有一层织物，不必更新边界条件
			{
				return;
			}

			if(layNumber==0)//最内层织物
			{
				fabric=(TNumericalFabric)fabricList.get(layNumber);
				fabricNext=(TNumericalFabric)fabricList.get(layNumber+1);//相邻的织物

				fabricNext.m_temperatureInner=(fabric.m_tFabric[fabric.m_nx-1].preNodeValue +fabric.m_tFabric[fabric.m_nx-2].preNodeValue
					+fabricNext.m_tFabric[0].preNodeValue +fabricNext.m_tFabric[1].preNodeValue )/4.0;

				fabricNext.m_RHInner=(fabric.m_RH[fabric.m_nx-1]+fabric.m_RH[fabric.m_nx-2]
				+fabricNext.m_RH[0]+fabricNext.m_RH[1])/4.0;

				if(fabric.m_modelFlag.compareTo(ModelType.AddLiquid)>=0)
				{
					fabricNext.m_vLiquidInner =(fabric.m_vLiquidWater[fabric.m_nx-1].preNodeValue +fabric.m_vLiquidWater[fabric.m_nx-2].preNodeValue
						+fabricNext.m_vLiquidWater[0].preNodeValue +fabricNext.m_vLiquidWater[1].preNodeValue )/4.0;
				}

			}
			else if(layNumber==fabricList.size()-1)//最外层织物
			{
				fabricPre=(TNumericalFabric)fabricList.get(layNumber-1);
				fabric=(TNumericalFabric)fabricList.get(layNumber);

				fabricPre.m_temperatureOuter=(fabricPre.m_tFabric[fabric.m_nx-1].preNodeValue +fabricPre.m_tFabric[fabric.m_nx-2].preNodeValue
					+fabric.m_tFabric[0].preNodeValue +fabric.m_tFabric[1].preNodeValue )/4.0;
				fabricPre.m_RHOuter=(fabricPre.m_RH[fabric.m_nx-1]+fabricPre.m_RH[fabric.m_nx-2]
				+fabric.m_RH[0]+fabric.m_RH[1])/4.0;
				if(fabric.m_modelFlag.compareTo(ModelType.AddLiquid)>=0)
				{
					fabricPre.m_vLiquidOuter =(fabricPre.m_vLiquidWater[fabric.m_nx-1].preNodeValue +fabricPre.m_vLiquidWater[fabric.m_nx-2].preNodeValue
						+fabric.m_vLiquidWater[0].preNodeValue +fabric.m_vLiquidWater[1].preNodeValue )/4.0;

				}
			}
			else//中间织物，相邻织物有两个
			{
				fabricPre=(TNumericalFabric)fabricList.get(layNumber-1);
				fabric=(TNumericalFabric)fabricList.get(layNumber);
				fabricNext=(TNumericalFabric)fabricList.get(layNumber+1);

				fabricPre.m_temperatureOuter=(fabricPre.m_tFabric[fabric.m_nx-1].preNodeValue +fabricPre.m_tFabric[fabric.m_nx-2].preNodeValue
					+fabric.m_tFabric[0].preNodeValue +fabric.m_tFabric[1].preNodeValue )/4.0;
				fabricNext.m_temperatureInner=(fabric.m_tFabric[fabric.m_nx-1].preNodeValue +fabric.m_tFabric[fabric.m_nx-2].preNodeValue
					+fabricNext.m_tFabric[0].preNodeValue +fabricNext.m_tFabric[1].preNodeValue )/4.0;

				fabricPre.m_RHOuter=(fabricPre.m_RH[fabric.m_nx-1]+fabricPre.m_RH[fabric.m_nx-2]
				+fabric.m_RH[0]+fabric.m_RH[1])/4.0;
				fabricNext.m_RHInner=(fabric.m_RH[fabric.m_nx-1]+fabric.m_RH[fabric.m_nx-2]
				+fabricNext.m_RH[0]+fabricNext.m_RH[1])/4.0;

				if(fabric.m_modelFlag.compareTo(ModelType.AddLiquid)>=0)
				{
					fabricPre.m_vLiquidOuter =(fabricPre.m_vLiquidWater[fabric.m_nx-1].preNodeValue +fabricPre.m_vLiquidWater[fabric.m_nx-2].preNodeValue
						+fabric.m_vLiquidWater[0].preNodeValue +fabric.m_vLiquidWater[1].preNodeValue )/4.0;

					fabricNext.m_vLiquidInner =(fabric.m_vLiquidWater[fabric.m_nx-1].preNodeValue +fabric.m_vLiquidWater[fabric.m_nx-2].preNodeValue
						+fabricNext.m_vLiquidWater[0].preNodeValue +fabricNext.m_vLiquidWater[1].preNodeValue )/4.0;

				}
			}
		}
	};
	
	void SetBoundaryTemperature(StageType stage,int stageNumber) {
		TNumericalFabric fabric;
		TNumericalGarment garment;

		for(int k=0;k<m_bodySegment;k++)
		{
			if(stage.BodyGarment[k]>-1)
			{
				garment = (TNumericalGarment)m_bodyGarmentList[k].get(stageNumber);
				fabric = (TNumericalFabric)garment.m_fabricList.get(0);
				if(m_modelSelected.bodyModel)
				{
					fabric.m_temperatureInner = m_humanbody.m_tSkin[k];
					fabric.m_CVaporInner = m_humanbody.m_cWaterVapor[k];
				}
			}
		}
	};

	BoundarySeg SetBoundarySegmentCondition(int segNo,final BoundaryHuman human, final BoundaryEnv env, final ArrayList<BoundaryFabric> fabric)
	{
		BoundarySeg seg = new BoundarySeg();
		seg.m_segment = segNo;
		seg.m_BHuman = human;
		seg.m_BEnv = env;
		seg.m_Bfabric = fabric;
		return seg;
	};
	
	boolean SetBoundarySegmentCondition(StageType stage, int segNo) {
		int index = (int)segNo;
		boolean b_Exist=false;
		BoundarySeg pSeg = null;
		for(int i=0;i<m_boundarySeg[index].size();++i)
		{
			if(m_boundarySeg[index].get(i).m_stageName.equals(stage.StageName))
			{
				pSeg = m_boundarySeg[index].get(i);
				b_Exist = true;
				break;		
			}
		}
		if(!b_Exist)
		{
			pSeg = new BoundarySeg();
			pSeg.m_BHuman.m_heatCoeUncover = 0.000239;
			pSeg.m_BHuman.m_heatCoeCover = 0.000239;
			pSeg.m_BHuman.m_radCoeUncover = 0.0001434;
			pSeg.m_BHuman.m_radCoeCover = 0.0001434;
			pSeg.m_BEnv.m_radiation = 0;
			pSeg.m_BEnv.m_liquidFraction = 2E-20;
		}
		pSeg.m_BHuman.m_metabolicRate = (int)stage.Work;
		pSeg.m_BEnv.m_temperature = stage.InTemperature;
		pSeg.m_BEnv.m_humidity = stage.InRH;
		pSeg.m_BEnv.m_velocity = stage.InVelocity;
		pSeg.m_segment = segNo;
		pSeg.m_stageName = stage.StageName;

		if(!b_Exist)
			m_boundarySeg[index].add(pSeg);
		return true;
	}

	void SetModelSelectionParameters() {
		m_modelSelected.localTemperature = true;
		m_modelSelected.nonLocalTemperature = false;
		m_modelSelected.liquid = true;
		m_modelSelected.wateVapor = true;
		m_modelSelected.pressure = true;
		m_modelSelected.virus = false;
		m_modelSelected.membrane = false;
		m_modelSelected.PCM = false;
		m_modelSelected.selfHeating = false;
		m_modelSelected.radiation = true;
		m_modelSelected.bodyModel = true;
		m_modelSelected.babyModel = false;
		m_modelSelected.comfort = false;
	}
	
	void SetBodyParameters() {
		//m_body = new BodyType();
		m_body.CCON=10.8;
		m_body.SCON=10.8;
		m_body.PCON=0.0;
		m_body.Res=333.3;
		m_body.CCHIL=13.0;
		m_body.PCHIL=1.0;
		m_body.SCHIL=0.40;
		m_body.CSW=372.0;
		m_body.SSW=33.7;
		m_body.CDIL=136.0;
		m_body.PSW=0.0;
		m_body.SDIL=17.0;
		m_body.PDIL=0.0;

		for(int i=0;i<25;i++)
		{
			m_body.C[i] = C[i];
			m_body.TSET[i] = TSET[i];
			m_body.T[i] = T[i];
		}

		for(int i=0;i<24;i++)
		{
			m_body.QB[i] = QB[i];
			m_body.EB[i] = EB[i];
			m_body.BFB[i] = BFB[i];
			m_body.TC[i] = TC[i];
		}

		for(int i=0;i<6;i++)
		{
			m_body.S[i] = S[i];
			m_body.HC[i] = HC[i];
			m_body.SKINR[i] = SKINR[i];
			m_body.SKINS[i] = SKINS[i];
			m_body.SKINV[i] = SKINV[i];
			m_body.SKINC[i] = SKINC[i];
			m_body.MWORK[i] = MWORK[i];
			m_body.MCHILL[i] = MCHILL[i];
		}
	}
	
	void PreSimulation() {
		/*ofstream simulateData;
		simulateData.open("E:\\simulation.sdf",ios::trunc);
		simulateData.setf(ios::left|ios::uppercase);
		SaveModel(simulateData);
		SaveFibers(simulateData);
		 
		SaveFabrics(simulateData);
		SaveGarment(simulateData);
		SaveBody(simulateData);
		SaveStage(simulateData);
		SaveControls(simulateData);*/
	}

    GarmentType GetGarment(String name) {
    	for(int i=0;i<m_oringinalGarmentList.size();i++)
    	{
    		if(m_oringinalGarmentList.get(i).GarmentName.equals(name))
    			return m_oringinalGarmentList.get(i);
    	}
    	return null;
    }
    //FabricType GetFabric(char[] name);
	
	void ClearAllData() {
		m_fiberList.clear();
		m_membraneList.clear();
		m_pcmList.clear();
		m_fabricList.clear();
		m_stageList.clear();
		m_garmentList.clear();
		m_oringinalGarmentList.clear();
		m_numerialGarmentList.clear();
	}
	
	boolean data_coming_flag;
	boolean core_temp_flag;
}
